<template>
  <view class="wrap">
    <view class="search">
      <u-search
        v-model="query.noticeTitle"
        @custom="search"
        @search="search"
        @clear="clear"
      ></u-search>
    </view>
    <scroll-view class="scroll-list msg-list-item" scroll-y="true" @scrolltolower="lower">
      <u-empty text="暂无数据" mode="list" v-if="dataList.length < 1"></u-empty>
      <view v-for="item in dataList" :key="item.id">
        <view class="msg-time">{{ item.createTime }}</view>
        <u-card
          :title="item.noticeTitle"
          sub-title=""
          padding="20"
          margin="0rpx 20rpx"
          thumb="/static/aidex/images/list-icon.png"
          @click="viewItem(item)"
        >
          <view class="" slot="body">
            <view class="u-body-item u-flex u-col-between u-p-t-0">
              <view class="u-body-item-title u-line-2">{{
                item.noticeContent.replace(/<[^>]*>/g, '')
              }}</view>
            </view>
          </view>
        </u-card>
      </view>
      <u-divider v-if="!listHasMore">已经到底了</u-divider>
    </scroll-view>
  </view>
</template>
<script>
import { processImgTags } from '../../../common/common.js'
export default {
  data() {
    return {
      listHasMore: true, //有无下一页数据
      page: {
        pageNum: 1,
        pageSize: 10,
      },
      query: {
        noticeTitle: '',
      },
      type: '', //新闻/通知
      dataList: [],
    }
  },
  onLoad(query) {
    this.type = query.type
    this.getList()
  },
  methods: {
    navTo(url) {
      uni.navigateTo({
        url: url,
      })
    },
    getList() {
      if (this.type == 'news') {
        this.$u.api
          .getNewsList({ ...this.page, ...this.query })
          .then((response) => {
            if (response.code == '200') {
              if (response.rows.length < 10) {
                this.listHasMore = false
              }
              let list = response.rows
              if (this.page.pageNum == 1) {
                this.dataList = []
              }
              let temp = JSON.parse(JSON.stringify(list))
              this.dataList = this.dataList.concat(temp)
              this.dataList.map((item) => {
                return {
                  ...item,
                  noticeContent: processImgTags(
                    item.noticeContent,
                    this.vuex_config.baseUrl
                  ),
                }
              })
            } else {
              this.$u.toast(response.msg)
            }
          })
      } else {
        this.$u.api
          .getNoticeCount({ ...this.page, ...this.query })
          .then((response) => {
            if (response.code == '200') {
              if (response.rows.length < 10) {
                this.listHasMore = false
              }
              let list = response.rows
              if (this.page.pageNum == 1) {
                this.dataList = []
              }
              let temp = JSON.parse(JSON.stringify(list))
              this.dataList = this.dataList.concat(temp)
              this.dataList.map((item) => {
                return {
                  ...item,
                  noticeContent: processImgTags(
                    item.noticeContent,
                    this.vuex_config.baseUrl
                  ),
                }
              })
              // console.log(this.dataList)
            } else {
              this.$u.toast(response.msg)
            }
          })
      }
    },
    lower: function (e) {
      if (!this.listHasMore) {
        return
      }
      this.page.pageNum++
      this.getList()
    },
    search(value) {
      this.query.noticeTitle = value
      this.getList()
    },
    clear() {
      this.query.noticeTitle = ''
      this.getList()
    },
    viewItem(row) {
      uni.navigateTo({ url: `/pages/sys/msg/form?id=${row.noticeId}` })
    },
    // processImgTags(richText, prefix) {
    //   return richText.replace(/<img([^>]+)>/gi, (match, p1) => {
    //     // 查找 src 属性
    //     const srcRegex = /src=["']([^"']+)["']/
    //     const srcMatch = match.match(srcRegex)

    //     if (srcMatch) {
    //       const originalSrc = srcMatch[1]
    //       const newSrc = prefix + originalSrc

    //       // 替换 src 属性值
    //       const imgTagWithNewSrc = match.replace(srcMatch[0], `src="${newSrc}"`)

    //       // 检查是否有 style 属性
    //       if (p1.includes('style=')) {
    //         // 如果有 style 属性，追加宽度属性
    //         return imgTagWithNewSrc.replace(
    //           '>',
    //           ' mode="widthFix" style="width:50px;">'
    //         )
    //       } else {
    //         // 如果没有 style 属性，添加一个新的 style 属性和 mode 属性
    //         return imgTagWithNewSrc.replace(
    //           '>',
    //           ' style="width:50px;" mode="widthFix">'
    //         )
    //       }
    //     }

    //     return match
    //   })
    // },
  },
}
</script>
<style lang="scss">
@import '../../../common/uni.css';
page {
  background-color: #f5f5f5;
}
.wrap .search {
  background: #ffffff;
}
.msg-time {
  font-size: 26rpx;
  padding: 10px 0;
  color: #999999;
  text-align: center;
}
.u-card__foot {
  .u-icon {
    margin-right: 10px;
  }
}
</style>
